package com.symbio.ishenapi.background.controller;

import com.symbio.ishenapi.background.model.TmDiseaseBase;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.io.File;
import java.util.Date;
import java.util.Map;

/**
 * @Author: chen
 * @Description:疾病管理控制层
 * @Date: Created in 9:44 2018/10/23
 * @Modified By:
 */
@RequestMapping("/TmDiseaseBase")
public class TmDiseaseBaseController extends BaseController {

    /**
     * 疾病列表查询
     */
    public void findList(){

        Map map = TmDiseaseBase.dao.findList(getQueryModel());
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    public void findNameList(){
        String diseaseName = getPara("diseaseName");
        Map map = TmDiseaseBase.dao.findNameList(getQueryModel(),diseaseName);
        renderPageJsonRet(map.get("count"),map.get("data"));
    }

    /**
     * 疾病添加
     */
    public void save(){
        TmDiseaseBase tmDiseaseBase = (TmDiseaseBase)getReqBodyObject(TmDiseaseBase.class);
        //填充数据
        tmDiseaseBase.setCreateTime(new Date());
        tmDiseaseBase.setStatus("1");

        if(tmDiseaseBase.getDiseaseCode()==null||"".equals(tmDiseaseBase.getDiseaseCode())){
            renderJsonError(40003, "疾病编码");
        }
        if(tmDiseaseBase.getDiseaseName()==null||"".equals(tmDiseaseBase.getDiseaseName())){
            renderJsonError(40003, "疾病名称");
        }

        if (tmDiseaseBase.save())
            renderJsonRet(40001);
        else
            renderJsonError(40002);
    }

    /**
     * 疾病逻辑删
     */
    public void delete(){
        TmDiseaseBase tmDiseaseBase = (TmDiseaseBase)getReqBodyObject(TmDiseaseBase.class);
        if(TmDiseaseBase.dao.deleteDiseaseBase(tmDiseaseBase) > 0) {
            renderJsonRet(40004);
        }else{
            renderJsonError(40005);
        }
    }

    /**
     * 疾病编辑
     */
    public void update(){
        TmDiseaseBase tmDiseaseBase = (TmDiseaseBase)getReqBodyObject(TmDiseaseBase.class);
        TmDiseaseBase dbTmDiseaseBase = TmDiseaseBase.dao.findById(tmDiseaseBase.getId());
        if(dbTmDiseaseBase!=null){
            if(tmDiseaseBase.getDiseaseCode()==null||"".equals(tmDiseaseBase.getDiseaseCode())){
                renderJsonError(40003, "疾病编码");
            }
            if(tmDiseaseBase.getDiseaseName()==null||"".equals(tmDiseaseBase.getDiseaseName())){
                renderJsonError(40003, "疾病名称");
            }
            dbTmDiseaseBase.setDiseaseCode(tmDiseaseBase.getDiseaseCode());
            dbTmDiseaseBase.setDiseaseName(tmDiseaseBase.getDiseaseName());
        }

        if (tmDiseaseBase.update())
            renderJsonRet(40004);
        else
            renderJsonError(40005);

    }


    /**
     * 疾病初始化   Excel导入
     */
    public void initData(){
        File file = getParamFile();
        if (TmDiseaseBase.dao.initData(file) > 0)
            renderJsonRet(40001);
        else
            renderJsonError(40002);
    }
}
